package com.example.baidumapdemo.utils;

import android.os.Build;

import androidx.annotation.RequiresApi;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.example.baidumapdemo.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class BaiduMapUtils {

    public static void navigateTo(BaiduMap baiduMap, float zoom, LatLng latLng) {
        MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(latLng);
        baiduMap.animateMapStatus(update);
        update = MapStatusUpdateFactory.zoomTo(zoom);
        baiduMap.animateMapStatus(update);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public static void navigateTo(BaiduMap baiduMap, Integer width, Integer height, LatLng... latLngList) {
        System.out.println(latLngList.length);
        if (latLngList.length > 1) {
            List<LatLng> latLngs = Arrays.asList(latLngList);
            List<Double> longitudeList = latLngs.stream().map(latLng -> latLng.longitude).collect(Collectors.toList());
            List<Double> latitudeList = latLngs.stream().map(latLng -> latLng.latitude).collect(Collectors.toList());
            LatLng rightTopLatLng = new LatLng(Collections.max(latitudeList), Collections.min(longitudeList));
            LatLng leftBottomLatLng = new LatLng(Collections.min(latitudeList), Collections.max(longitudeList));
            LatLngBounds latLngBounds = new LatLngBounds.Builder().include(rightTopLatLng).include(leftBottomLatLng).build();
            MapStatusUpdate update;
            if (width != null && height != null) {
                update = MapStatusUpdateFactory.newLatLngBounds(latLngBounds, width, height);
            } else {
                update = MapStatusUpdateFactory.newLatLngBounds(latLngBounds);
            }
            baiduMap.animateMapStatus(update);
        }
    }

    /**
     * 地图显示自己的位置，前提已经开启了地图定位图层:BaiduMap.setMyLocationEnabled(true)
     *
     * @param location
     * @param baiduMap
     */
    public static void locateTo(BDLocation location, BaiduMap baiduMap) {
        double latitude = location.getLatitude();    //获取纬度信息
        double longitude = location.getLongitude();    //获取经度信息
        float radius = location.getRadius();    //获取定位精度，默认值为0.0f
        float direction = location.getDirection(); //???
        String coorType = location.getCoorType();
        //获取经纬度坐标类型，以LocationClientOption中设置过的坐标类型为准

        int errorCode = location.getLocType();
        //获取定位类型、定位错误返回码，具体信息可参照类参考中BDLocation类中的说明
        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(radius)
                // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(direction).latitude(latitude)
                .longitude(longitude).build();
        baiduMap.setMyLocationData(locData);
    }

    public static void markPoint(BaiduMap baiduMap, LatLng point) {
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.icon_marka);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        baiduMap.addOverlay(option);
    }

    public static void markPoint(BaiduMap baiduMap, LatLng point, int icon) {
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(icon);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        baiduMap.addOverlay(option);
    }
}
